package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.dao.PageDecorationDao;
import com.ytjj.qmyx.admin.model.request.*;
import com.ytjj.qmyx.admin.model.response.DecorationResponse;
import com.ytjj.qmyx.admin.model.response.PageDecorationResponse;
import com.ytjj.qmyx.admin.service.PageDecorationService;
import com.ytjj.qmyx.mall.model.PageDecoration;
import com.ytjj.qmyx.mall.model.PageDecorationImg;
import com.ytjj.qmyx.mall.model.PageDecorationProduct;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 *
 * @Version: 1.0
 */
@Service
public class PageDecorationServiceImpl implements PageDecorationService {

	@Resource
	private PageDecorationDao pageDecorationDao;

	@Override
	public CommonPage<PageDecoration> getList(PageDecorationRequest request) {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		return CommonPage.restPage(pageDecorationDao.getList(request.getTitle()));
	}

	@Override
	public CommonPage<PageDecorationResponse> selectList(PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		return CommonPage.restPage(pageDecorationDao.selectList());
	}

	@Override
	public List<PageDecorationResponse> searchProduct(String productName) {
		return pageDecorationDao.searchProduct(productName);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertPage(DecorationRequest[] request) {
		if(request.length > 0) {
			List<DecorationRequest> requestList = Arrays.asList(request);

			DecorationRequest decoration = requestList.get(0);
			PageDecoration page = new PageDecoration();
			page.setTitle(decoration.getTitle());
			page.setBackColor(decoration.getBackColor());
			page.setRemarks(decoration.getRemarks());
			pageDecorationDao.pageTitle(page);
			// 当前增加的页面id
			Integer pageId = page.getId();

			int currIndex = 0;
			for(DecorationRequest decora : requestList) {
				// 页面装修-增加商品
				if(decora.getType().equals("product")) {
					PageDecorationProduct product = new PageDecorationProduct();
					product.setPageId(pageId);
					product.setProductId(decora.getData());
					product.setOriginalFlag(decora.getOptions().isOriginalPrice());
					product.setGoodRatio(decora.getOptions().isGoodRatio());
					product.setVolumeStr(decora.getOptions().isVolumeStr());
					product.setTabType(decora.getTabType());
					product.setSubscript(currIndex);
					pageDecorationDao.pageProduct(product);
				}
				if(decora.getType().equals("images")) {
					for(PageImgRequest imgRequest : decora.getImgRequests()) {
						PageDecorationImg images = new PageDecorationImg();
						images.setPageId(pageId);
						images.setType("images");
						images.setName(imgRequest.getName());
						images.setUrl(imgRequest.getUrl());
						images.setLink(imgRequest.getLink());
						images.setTabType(decora.getTabType());
						images.setSubscript(currIndex);
						pageDecorationDao.pageImg(images);
					}
				}
				if(decora.getType().equals("banner")) {
					for(PageImgRequest imgRequest : decora.getImgRequests()) {
						PageDecorationImg images = new PageDecorationImg();
						images.setPageId(pageId);
						images.setType("banner");
						images.setName(imgRequest.getName());
						images.setUrl(imgRequest.getUrl());
						images.setLink(imgRequest.getLink());
						images.setTabType(null);
						images.setSubscript(currIndex);
						pageDecorationDao.pageImg(images);
					}
				}
				currIndex += 1;
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public List<DecorationResponse> decoraList(Integer id) {
		List<DecorationResponse> decoraList = new ArrayList<>();

		// type: info
		DecorationResponse decoInfo = new DecorationResponse();
		// 获取页面装修数据
		PageDecoration pageDecoration = pageDecorationDao.getPageDecoration(id);
		decoInfo.setType("info");
		decoInfo.setTitle(pageDecoration.getTitle());
		decoInfo.setBackColor(pageDecoration.getBackColor());
		decoInfo.setRemarks(pageDecoration.getRemarks());
		decoInfo.setSort(0);
		decoraList.add(decoInfo);

		// type: product
		// 获取页面装修-商品数据
		List<Integer> proSubscript = pageDecorationDao.getProSubscript(id);
		if (!CollectionUtils.isEmpty(proSubscript)) {
			for (Integer proSub : proSubscript) {
				PageDecorationProduct pageDecoProduct = pageDecorationDao.getPageProduct(id, proSub);
				DecorationResponse pageProduct = new DecorationResponse();
				pageProduct.setType("product");
				String[] productId = pageDecoProduct.getProductId().split(",");
				List<PageDecorationResponse> productList = new ArrayList<>();
				for(String proId :productId) {
					productList.add(pageDecorationDao.getProductInfo(Integer.parseInt(proId)));
				}
				pageProduct.setProductInfo(productList);
				OptionsRequest options = new OptionsRequest();
				options.setOriginalPrice(pageDecoProduct.getOriginalFlag());
				options.setGoodRatio(pageDecoProduct.getGoodRatio());
				options.setVolumeStr(pageDecoProduct.getVolumeStr());
				pageProduct.setOptions(options);
				pageProduct.setTabType(pageDecoProduct.getTabType());
				pageProduct.setSort(pageDecoProduct.getSubscript());
				decoraList.add(pageProduct);

			}
		}

		// type: images
		// 获取页面装修-images图片数据
		List<Integer> imagesSub = pageDecorationDao.getImgSubscript(id, "images");
		if (!CollectionUtils.isEmpty(imagesSub)) {
			for (Integer sub : imagesSub) {
				DecorationResponse decoImages = new DecorationResponse();
				decoImages.setType("images");
				List<PageImgRequest> imgRequests = new ArrayList<>();
				List<PageDecorationImg> imgList = pageDecorationDao.getPageImg(id, "images", sub);
				if (!CollectionUtils.isEmpty(imgList)) {
					for(PageDecorationImg pdi : imgList) {
						PageImgRequest img = new PageImgRequest();
						img.setName(pdi.getName());
						img.setUrl(pdi.getUrl());
						img.setLink(pdi.getLink());
						imgRequests.add(img);
						decoImages.setTabType(pdi.getTabType());
						decoImages.setSort(pdi.getSubscript());
					}
				}
				decoImages.setImgRequests(imgRequests);
				decoraList.add(decoImages);
			}
		}

		// type: banner
		// 获取页面装修-banner轮播图数据
		List<Integer> bannerSub = pageDecorationDao.getImgSubscript(id, "banner");
		if (!CollectionUtils.isEmpty(bannerSub)) {
			for(Integer subBanner : bannerSub) {
				DecorationResponse decoBannner = new DecorationResponse();
				decoBannner.setType("banner");
				List<PageImgRequest> imgRequests = new ArrayList<>();
				List<PageDecorationImg> bannerList = pageDecorationDao.getPageImg(id, "banner", subBanner);
				if (!CollectionUtils.isEmpty(bannerList)) {
					for(PageDecorationImg bdi : bannerList) {
						PageImgRequest img = new PageImgRequest();
						img.setName(bdi.getName());
						img.setUrl(bdi.getUrl());
						img.setLink(bdi.getLink());
						imgRequests.add(img);
						decoBannner.setTabType(bdi.getTabType());
						decoBannner.setSort(bdi.getSubscript());
					}
				}
				decoBannner.setImgRequests(imgRequests);
				decoraList.add(decoBannner);
			}
		}

		return decoraList;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updatePageById(DecorationRequest[] request) {
		if (request.length > 0) {
			List<DecorationRequest> requestList = Arrays.asList(request);

			// type: info(默认下标为0)
			DecorationRequest decoration = requestList.get(0);
			// 页面装修主表id
			Integer pageId = decoration.getId();
			pageDecorationDao.updatePage(pageId, decoration.getTitle(), decoration.getBackColor(), decoration.getRemarks());

			// 执行删除后增加新的页面商品，图片
			this.deleteImgPro(pageId);

			//执行插入操作
			int currIndex = 0;
			for(DecorationRequest decora : requestList) {
				// 页面装修-增加商品
				if(decora.getType().equals("product")) {
					PageDecorationProduct product = new PageDecorationProduct();
					product.setPageId(pageId);
					product.setProductId(decora.getData());
					product.setOriginalFlag(decora.getOptions().isOriginalPrice());
					product.setGoodRatio(decora.getOptions().isGoodRatio());
					product.setVolumeStr(decora.getOptions().isVolumeStr());
					product.setTabType(decora.getTabType());
					product.setSubscript(currIndex);
					pageDecorationDao.pageProduct(product);
				}
				if(decora.getType().equals("images")) {
					for(PageImgRequest imgRequest : decora.getImgRequests()) {
						PageDecorationImg images = new PageDecorationImg();
						images.setPageId(pageId);
						images.setType("images");
						images.setName(imgRequest.getName());
						images.setUrl(imgRequest.getUrl());
						images.setLink(imgRequest.getLink());
						images.setTabType(decora.getTabType());
						images.setSubscript(currIndex);
						pageDecorationDao.pageImg(images);
					}
				}
				if(decora.getType().equals("banner")) {
					for(PageImgRequest imgRequest : decora.getImgRequests()) {
						PageDecorationImg images = new PageDecorationImg();
						images.setPageId(pageId);
						images.setType("banner");
						images.setName(imgRequest.getName());
						images.setUrl(imgRequest.getUrl());
						images.setLink(imgRequest.getLink());
						images.setTabType(null);
						images.setSubscript(currIndex);
						pageDecorationDao.pageImg(images);
					}
				}
				currIndex += 1;
			}
		}
	}

	public void deleteImgPro(Integer pageId) {
		// 删除图片
		pageDecorationDao.deleteImg(pageId);
		// 删除商品
		pageDecorationDao.deleteProduct(pageId);
	}

	@Override
	public void updateStatus(Integer id) {
		pageDecorationDao.updateStatus(id, 3);
	}
}
